// 完善后的登录注册代码
#include "login.h"
#include "keyboard.h"
#include <string.h>
#include <stdbool.h>
#include "tcp_client.h"

LV_FONT_DECLARE(lv_font_sunti_24);
LV_FONT_DECLARE(lv_font_fan_24);
LV_FONT_DECLARE(lv_font_boss_24);

// 全局UI元素
lv_obj_t *ui_labelmessage;        // 登录界面提示标签
lv_obj_t *ui_labelRegMessage;     // 注册界面提示标签

// 账号密码存储
char correct_username[50] = "";
char correct_password[50] = "";
bool has_registered = false;

// 声明定时器回调
static void register_success_timer_cb(lv_timer_t *t);
// 声明所有回调函数（避免未定义错误）
void btn_login_event_cb(lv_event_t * e);
void btn_goto_register_event_cb(lv_event_t *e);
void btn_register_confirm_event_cb(lv_event_t * e);

// -------------------------- 登录界面 --------------------------
void login_init() {
    // 创建登录提示标签
    ui_labelmessage = lv_label_create(ui_screenlogin); 
    lv_obj_set_pos(ui_labelmessage, 100, 80);
    lv_label_set_text(ui_labelmessage, "请先注册账号");
    lv_obj_set_style_text_font(ui_labelmessage, &lv_font_sunti_24, 0);
    lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0x000000), 0);

    // 登录输入框绑定键盘事件
    lv_obj_add_event_cb(ui_tausername, ta_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_tapassword, ta_event_cb, LV_EVENT_CLICKED, NULL);
    
 
    // 登录按钮事件
    lv_obj_add_event_cb(ui_btnlogin, btn_login_event_cb, LV_EVENT_CLICKED, NULL);
    
    
}


// 登录按钮事件
void btn_login_event_cb(lv_event_t * e) {
    const char *username = lv_textarea_get_text(ui_tausername);
    const char *password = lv_textarea_get_text(ui_tapassword);

    if (strlen(username) == 0 || strlen(password) == 0) {
        lv_label_set_text(ui_labelmessage, "用户名或密码不能为空");
        lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0xFF0000), 0);
        return;
    }

    if (!has_registered) {
        lv_label_set_text(ui_labelmessage, "请先注册账号");
        lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0xFF0000), 0);
        return;
    }

    if (strcmp(username, correct_username) == 0 && strcmp(password, correct_password) == 0) {
        lv_label_set_text(ui_labelmessage, "登录成功");
        lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0x00FF00), 0);
        tcp_send_login_success(username);
        lv_scr_load(ui_Screen4);
    } else {
        lv_label_set_text(ui_labelmessage, "账号密码错误");
        lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0xFF0000), 0);
    }
    lv_textarea_set_text(ui_tapassword, "");
}



// -------------------------- 注册界面 --------------------------
void register_init() {
   
    
    // 注册输入框绑定键盘事件
    lv_obj_add_event_cb(ui_taRegUsername, ta_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_taRegPassword, ta_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_taRegConfirmPassword, ta_event_cb, LV_EVENT_CLICKED, NULL);
    // 确保注册输入框可点击
    lv_obj_add_flag(ui_taRegUsername, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_flag(ui_taRegPassword, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_flag(ui_taRegConfirmPassword, LV_OBJ_FLAG_CLICKABLE);
    
    // 注册确认按钮事件
    lv_obj_add_event_cb(ui_btnRegisterConfirm, btn_register_confirm_event_cb, LV_EVENT_CLICKED, NULL);
    
}

// 注册成功定时器回调
static void register_success_timer_cb(lv_timer_t *t) {
    lv_scr_load(ui_screenlogin);
    reset_keyboard();
    lv_label_set_text(ui_labelmessage, "请输入账号密码");
    lv_obj_set_style_text_color(ui_labelmessage, lv_color_hex(0x000000), 0);
    lv_timer_del(t);
}

// 注册确认事件
void btn_register_confirm_event_cb(lv_event_t * e) {
    const char *username = lv_textarea_get_text(ui_taRegUsername);
    const char *password = lv_textarea_get_text(ui_taRegPassword);
    const char *confirm_password = lv_textarea_get_text(ui_taRegConfirmPassword);
    
    if(strlen(username) == 0 || strlen(password) == 0) {
        lv_label_set_text(ui_labelRegMessage, "用户名和密码不能为空");
        lv_obj_set_style_text_font(ui_labelRegMessage, &lv_font_sunti_24, 0);
        lv_obj_set_style_text_color(ui_labelRegMessage, lv_color_hex(0xFF0000), 0);
        return;
    }
    
    if(strlen(username) > 10 || strlen(password) > 10) {
        lv_label_set_text(ui_labelRegMessage, "用户名或密码太长");
        lv_obj_set_style_text_font(ui_labelmessage, &lv_font_sunti_24, 0);
        lv_obj_set_style_text_color(ui_labelRegMessage, lv_color_hex(0xFF0000), 0);
        return;
    }
    
    if(strcmp(password, confirm_password) != 0) {
        lv_label_set_text(ui_labelRegMessage, "两次输入的密码不一致");
        lv_obj_set_style_text_font(ui_labelRegMessage, &lv_font_fan_24, 0);
        lv_obj_set_style_text_color(ui_labelRegMessage, lv_color_hex(0xFF0000), 0);
        return;
    }
    
    strncpy(correct_username, username, 49);
    strncpy(correct_password, password, 49);
    has_registered = true;

    // 注册成功后，发送消息到服务器（新增）
    tcp_send_register_success(username, password);
    
    lv_label_set_text(ui_labelRegMessage, "注册成功即将返回登录");
    lv_obj_set_style_text_font(ui_labelRegMessage, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(ui_labelRegMessage, lv_color_hex(0x555555), 0);
    
    lv_timer_t *timer = lv_timer_create(register_success_timer_cb, 1500, NULL);
}
